/*
 * Rectangle.cpp
 *
 *  Created on: 22-Feb-2014
 *      Author: dev
 */

#include "Rectangle.h"

namespace tf {

Rectangle::Rectangle() {
	x = y = width = height = 0;
}

Rectangle::Rectangle(int x, int y, size_t width, size_t height) {
	this->x = x;
	this->y = y;
	this->width = width;
	this->height = height;

	// set corners
	corners.clear();
	corners.push_back(new Point(x, y));
	corners.push_back(new Point(x + width, y));
	corners.push_back(new Point(x + width, y + height));
	corners.push_back(new Point(x, y + height));
}

void Rectangle::set(int x1, int y1, int x2, int y2) {
	x = x1;
	y = y1;
	width = x2 - x1;
	height = y2 - y1;

	// set corners
	corners.clear();
	corners.push_back(new Point(x, y));
	corners.push_back(new Point(x + width, y));
	corners.push_back(new Point(x + width, y + height));
	corners.push_back(new Point(x, y + height));
}

bool Rectangle::intersect(Rectangle rect) {

	if (rect.top() > bottom() || rect.bottom() < top()) {
		return false;
	}

	if (rect.left() > right() || rect.right() < left()) {
		return false;
	}

	return true;
}

int Rectangle::top() {
	return y;
}
int Rectangle::right() {
	return x + width;
}
int Rectangle::bottom() {
	return y + height;
}
int Rectangle::left() {
	return x;
}

string Rectangle::str() {
	stringstream ss;
	ss << "[";
	ss << "" << x;
	ss << ", " << y;
	ss << ", " << width;
	ss << ", " << height;
	ss << "]";

	return ss.str();
}

Rectangle::~Rectangle() {
	for (size_t i = 0; i < corners.size(); i++) {
		delete corners[i];
	}
}

} /* namespace tf */
